package com.es.hr.salaryParameter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import com.es.util.db.DBBean;
import com.es.util.db.Dao;

public class SalaryParameterBiz {
	
	private DBBean dbb = null;
	private Connection conn = null;
	private PreparedStatement prepared = null;
	private ResultSet resultSet = null;
	private Statement statement = null;	
	public boolean CreateEmpInfoDo(HashMap<String,String[]> map) throws Exception{
		String delSql = "delete from  hrm_salary_parameter";
		String inserSql = "insert into hrm_salary_parameter(salary_type,salary_percent,paramete_start,paramete_end,paramete_percent,paramete_type) values(?,?,?,?,?,?)";
		
		try {
			dbb = new DBBean();
			conn = dbb.getConn();
			conn.setAutoCommit(false);
			prepared = conn.prepareStatement(delSql);
			prepared.executeUpdate();
			prepared = conn.prepareStatement(inserSql);
			
			String[] tdIndex = map.get("tdIndex");
			for(int i=0;i<tdIndex.length;i++){
				String salary_percent = map.get("salary_percent"+tdIndex[i])[0];
				String salary_type = map.get("salary_type"+tdIndex[i])[0];
				String paramete_type = map.get("paramete_type"+tdIndex[i])[0];
				for(int j=0;j<map.get("paramete_start"+tdIndex[i]).length;j++){
					String paramete_start = map.get("paramete_start"+tdIndex[i])[j];				
					String paramete_end = map.get("paramete_end"+tdIndex[i])[j];
					String paramete_percent =map.get("paramete_percent"+tdIndex[i])[j];
					prepared.setString(1, salary_type);
					prepared.setString(2, salary_percent);
					prepared.setString(3, paramete_start);
					prepared.setString(4, paramete_end);
					prepared.setString(5, paramete_percent);
					prepared.setString(6, paramete_type);
					prepared.executeUpdate();
				}				
			}
			conn.commit();
		} catch(Exception e){
			System.out.print(e.getMessage());
				conn.rollback();
			   return false;
		   }finally {		   
			   close();
		    }
		    return true;
	}
	
	public ArrayList<HashMap<String,String>> getSalaryParameterList()throws Exception {

		ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
		Dao dao = new Dao();
		String sql = "select salary_type,salary_percent,paramete_start,paramete_end,paramete_percent,paramete_type from hrm_salary_parameter order by salary_type,salary_percent,paramete_type";
		try {
			ResultSet resultSet = dao.Select(sql);
			while(resultSet.next()){
				HashMap<String,String> map = new HashMap<String,String>();
				map.put("salary_type", resultSet.getString("salary_type"));
				map.put("salary_percent", resultSet.getString("salary_percent"));
				map.put("paramete_start", resultSet.getString("paramete_start"));
				map.put("paramete_end", resultSet.getString("paramete_end"));
				map.put("paramete_percent", resultSet.getString("paramete_percent"));
				map.put("paramete_type", resultSet.getString("paramete_type"));
				result.add(map);
			}
		} finally{
			dao.close();
		}
		return result;
	}
		
	public void close(){
	  if (this.resultSet != null){
		  try{
			  this.resultSet.close();
			  this.resultSet = null;
		  }catch (Exception ex){
			  ex.printStackTrace();
		  }
	  }
    
	  if (this.prepared != null){
		  try{
			  this.prepared.close();
			  this.prepared = null;
		  }catch (Exception ex){
			  ex.printStackTrace();
		  }
	  }
	  
	  if (this.statement != null){
		  try{
			  this.statement.close();
			  this.statement = null;
		  }catch (Exception ex){
			  ex.printStackTrace();
		  }
	  }
	  
	  if (this.conn != null){
		  try{
			  this.conn.close();
			  this.conn = null;
		  }catch (Exception ex){
			  ex.printStackTrace();
		  }
	  }
  }

}
